package com.papper.common.service.sys;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.papper.common.bean.sys.SysGroup;
import com.papper.common.bean.sys.SysLimit;
import com.papper.common.bean.sys.SysModel;
import com.papper.common.bean.sys.SysResource;
import com.papper.common.bean.sys.SysUser;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.mapper.sys.SysModelMapper;
import com.papper.common.service.ServiceImpl;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service("sysModelService")
public class SysModelService extends ServiceImpl<SysModelMapper, SysModel> {
	@Resource
	SysLimitService    sysLimitService;
	@Resource
	SysResourceService sysResourceService;

	/**
	 * 获取系统模块信息
	 *
	 * @param systemId   the system id
	 * @param dataStatus the data status
	 * @return the list
	 */
	public List<SysModel> findBySystemId(Long systemId, Integer dataStatus) {
		if (systemId == null) {
			return null;
		}
		Example          example  = new Example(SysModel.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("systemId", systemId);
		if (dataStatus != null) {
			criteria.andEqualTo("dataStatus", dataStatus);
		}
		if (StringUtil.isEmpty(example.getOrderByClause())) {
			example.setOrderByClause("model_sort asc");
		}
		return find(example);
	}

	/**
	 * 用户权限模块.
	 *
	 * @param systemId   the system id
	 * @param sysUser    the sys user
	 * @param dataStatus the data status
	 * @return the list
	 */
	public List<SysModel> findByLimit(Long systemId, SysUser sysUser, Integer dataStatus) {
		// 超级用户
		if (sysUser.getId() == 0) {
			return findBySystemId(systemId, dataStatus);
		}
		// 非当前登录系统
		if (systemId.intValue() != sysUser.getSystemId().intValue()) {
			return findBySystemId(systemId, dataStatus);
		}
		// 获取权限信息
		List<SysLimit> userLimits = sysLimitService.getUserLimits(sysUser);
		Set<String>    ids        = Sets.newHashSet();
		if (userLimits != null && !userLimits.isEmpty()) {
			for (SysLimit sysLimit : userLimits) {
				ids.add(sysLimit.getResId().toString());
			}
		}
		// 获取资源列表
		List<SysResource> sysResources = sysResourceService.findByIds(ids);
		Set<Integer>      modelIds     = Sets.newHashSet();
		if (sysResources != null && !userLimits.isEmpty()) {
			for (SysResource sysResource : sysResources) {
				if (sysResource.getDataStatus().intValue() == DataStatusEnum.CLOSE.getCode().intValue()) {
					continue;
				}
				modelIds.add(sysResource.getModelId());
			}
		}
		// 获取模块列表
		Example          example  = new Example(SysGroup.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("id", modelIds);
		if (dataStatus != null) {
			criteria.andEqualTo("dataStatus", dataStatus);
		}
		return find(example);
	}

	/**
	 * 删除模块及资源信息.
	 *
	 * @param id the id
	 */
	public void deleteWithRes(Long id) {
		SysModel sysModel = findById(id);
		if (sysModel != null) {
			List<SysResource> sysResourceList = sysResourceService.findByMid(sysModel.getId());
			Optional.ofNullable(sysResourceList).orElse(Lists.newArrayList()).forEach(sr -> {
				sysResourceService.deleteWithChild(sr);
			});
			delete(sysModel);
		}
	}
}